//
//  PHViewController.m
//  LastTemp
//
//  Created by apple on 15/4/24.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "PHViewController.h"
#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height
@interface PHViewController ()
@property(nonatomic,retain)UITableView *tableView;
@property(nonatomic,retain)NSMutableArray *arr;
@property(nonatomic,retain)UIActivityIndicatorView *activity;
@end

@implementation PHViewController
-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.arr = [NSMutableArray array];
    }
    [self urlData];
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"39"]];
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 49) style:(UITableViewStylePlain)];
    [self.view addSubview:self.tableView];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.rowHeight = 80;
    [_tableView release];
    self.tableView.backgroundColor = [UIColor clearColor];
    
    self.activity = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(WIDTH/2, 100, 30, 30)];
    [self.activity setCenter:CGPointMake(self.view.frame.size.width / 2, (self.view.frame.size.height - 113) / 2)]; // 指定进度轮中心点
    [self.activity setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhiteLarge];
    [self.view addSubview:self.activity];
    [self.activity startAnimating];
    [_activity release];
    
    
    
}
-(void)dealloc
{
    [_activity release];
    [_arr release];
    [_tableView release];
    [super dealloc];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.arr.count > 0) {
        [self.activity stopAnimating];
    }
    return self.arr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *str = @"reuse";
    PHCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
    if (cell == nil) {
        cell = [[[PHCell alloc] initWithStyle:(UITableViewCellStyleValue1) reuseIdentifier:str] autorelease];
    }
    cell.backgroundColor = [UIColor clearColor];
    PHModel *phModel = self.arr[indexPath.row];
    cell.nameLabel.text = phModel.name;
    cell.descLabel.text = phModel.desc;
    NSURL *url = [NSURL URLWithString:phModel.pic_url];
    [cell.pic_urlImageView sd_setImageWithURL:url];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    PHXQViewController *phVC = [[PHXQViewController alloc] init];
    PHModel *model = self.arr[indexPath.row];
    phVC.pic_url = model.pic_url;
    phVC.resid = model.resid;
    phVC.desc = model.desc;
    [self.navigationController pushViewController:phVC animated:YES];
    [phVC release];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}

//网络请求
-(void)urlData
{
    if ([AVSingleton shareSingleton].internetNum == 1) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"您现在没有网络,请检查网络!" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
        [alert show];
        [alert release];
    } else {
    NSString *str = @"http://iting.music.189.cn:9200/iting2/imusic/V2?catalogId=63393942&selfType=0&protocolCode=2.0&method=get_catalog_list&sid=234472872&networkType=wifi&serviceCode=100&sessionId=83356C7DD4CDA5C8EE072C41746AA3A6&format=json&timestamp=20150424142201&sign=209efce594ca2d52f60b122a14051872&imsi=87D618C0-A675-458D-BB55-BED13C712771&company=&appChannelCode=10000000&appVerCode=2.000.004";
    NSURL *url = [NSURL URLWithString:str];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        NSMutableDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        NSMutableArray *catalogBeanListArr = [dic objectForKey:@"catalogBeanList"];
        for (NSMutableDictionary *catalogBeanListDic in catalogBeanListArr) {
            PHModel *phModel = [[PHModel alloc] init];
            [phModel setValuesForKeysWithDictionary:catalogBeanListDic];
            [self.arr addObject:phModel];
            [phModel release];
        }
        [self.tableView reloadData];
    }];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
